import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Scanner;
public class TestAppointment {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int MAX_SIZE = 100;
		Appointment calendar[] = new Appointment[MAX_SIZE];
		Appointment loadData[] = new Appointment[MAX_SIZE];
		
		String type;
	    String description;
	    int time;
		int year;
	    String month;
	    int day;
	    String weekDay;
	    
	    boolean isContinue = true;
	   
        int insertPos;
        int count;
        
        String input;
        
		Scanner keyboard=new Scanner(System.in);
		File outFile = new File("SaveData.txt");
		File inFile = new File("LoadData.txt");
		
		Scanner inStream = null;
		try {
			inStream = new Scanner(inFile);
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		PrintWriter output = null;
		try {
			output = new PrintWriter(outFile);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		Date date1 = new Date(2010,"December",24,"Tuesday");
		Date date2 = new Date(2010, "August", 13, "Wednesday");
		Date date3 = new Date(2010, "March", 17, "Saturday");
		
        OneTime meetProf = new OneTime("Meet Professor", 1200, date1);
        Weekly doctor = new Weekly("See Doctor", 1400,  date2);
        Monthly eyeExam = new Monthly("Get Eyes Checked", 1500,  date3);
        Daily gym = new Daily("Go to Gym", 2100, date2);
        
        OneTime graduate = new OneTime("Graduate from High School", 1500, date1);
        Weekly classTime = new Weekly("Go to class", 1400, date3);
        Monthly meetParents = new Monthly("Go see your Parents", 900, date3);
        Daily brushTeeth = new Daily("Brush your teeth", 2100, date2);
        
        calendar[0] = meetProf;
        calendar[1] = doctor;
        calendar[2] = eyeExam;
        calendar[3] = gym;
        calendar[4] = graduate;
        calendar[5] = classTime;
        calendar[6] = meetParents;
        calendar[7] = brushTeeth;
        
        
        for(int i = 0; i < 8; i++){
        	calendar[i].displayAppointment();
        }
        
       
        
        while(isContinue){
        	
        	System.out.println("Do you want to search for an appointment?(y/n)");
        	input = keyboard.next();
        	
        	if(input.equalsIgnoreCase("n") || input.equalsIgnoreCase("no")){
        		break;
        	}else{
        		System.out.println("************************************");
        		System.out.println("Searching for Appointment...");
        		System.out.println("Please enter the year:");
        		year = keyboard.nextInt();
        	
        		System.out.println("Please enter the name of the month:");
        		month = keyboard.next();
        	
        		System.out.println("Please enter the day of the month:");
        		day = keyboard.nextInt();
        	
        		System.out.println("Please Enter the day of the week:");
        		weekDay = keyboard.next();
        	
        		for(int i = 0; i < 8; i++){
        			if(calendar[i].occursOn(year, month, day, weekDay)){
        				calendar[i].displayAppointment();
        			}
        		
        		}
        	}
        	
        }
        
        
        
        isContinue = true;
        insertPos = 8;
        
        //keyboard.nextLine();
        while(isContinue){
        	System.out.println("Do you wish to add an appointment?(y/n)");
        	input = keyboard.next();
        	if(input.equalsIgnoreCase("n") || input.equalsIgnoreCase("no")){
        		break;
        	}
        	
        	keyboard.nextLine();
        		
            System.out.println("************************************");	
        	System.out.println("Adding new Appointment...");
        	System.out.println("What type of appointment:");
        	type = keyboard.nextLine();
        	if(type.equalsIgnoreCase("One Time")){
        		OneTime oneTime = new OneTime();
        		
        		System.out.println("Please enter a description:");
        		description = keyboard.next();
        		oneTime.setDescription(description);
        		
        		System.out.println("Please enter a time:(military)");
        		time = keyboard.nextInt();
        		oneTime.setTime(time);
        		
        		System.out.println("Please enter the year:");
        		year = keyboard.nextInt();
        		
        		System.out.println("Please enter the name of the month:");
        		month = keyboard.next();
        		
        		System.out.println("Please enter the day:");
        		day = keyboard.nextInt();
        		
        		keyboard.nextLine();
        		System.out.println("Please enter the day of the week:");
        		weekDay = keyboard.nextLine();
        		
        		Date temp = new Date(year,month, time, weekDay);
        		oneTime.setDate(temp);
        		
        		calendar[insertPos] = oneTime;
        		insertPos++;
        		
        	}else if(type.equalsIgnoreCase("Daily")){
        		
        		Daily daily = new Daily();
        		
        		System.out.println("Please enter a description:");
        		description = keyboard.next();
        		daily.setDescription(description);
        		
        		System.out.println("Please enter a time:(military)");
        		time = keyboard.nextInt();
        		daily.setTime(time);
        		
        		System.out.println("Please enter the year:");
        		year = keyboard.nextInt();
        		
        		System.out.println("Please enter the name of the month:");
        		month = keyboard.next();
        		
        		System.out.println("Please enter the day:");
        		day = keyboard.nextInt();
        		
        		keyboard.nextLine();
        		System.out.println("Please enter the day of the week:");
        		weekDay = keyboard.nextLine();
        		
        		Date temp = new Date(year,month, time, weekDay);
        		daily.setDate(temp);
        		
        		calendar[insertPos] = daily;
        		insertPos++;
        		
        	}else if(type.equalsIgnoreCase("Weekly")){
        		
        		Weekly weekly= new Weekly();
        		
        		System.out.println("Please enter a description:");
        		description = keyboard.next();
        		weekly.setDescription(description);
        		
        		System.out.println("Please enter a time:(military)");
        		time = keyboard.nextInt();
        		weekly.setTime(time);
        		
        		System.out.println("Please enter the year:");
        		year = keyboard.nextInt();
        		
        		System.out.println("Please enter the name of the month:");
        		month = keyboard.next();
        		
        		System.out.println("Please enter the day:");
        		day = keyboard.nextInt();
        		
        		keyboard.nextLine();
        		System.out.println("Please enter the day of the week:");
        		weekDay = keyboard.nextLine();
        		
        		Date temp = new Date(year,month, time, weekDay);
        		weekly.setDate(temp);
        		
        		calendar[insertPos] = weekly;
        		insertPos++;
        		
        	}else if(type.equalsIgnoreCase("Monthly")){
        		
        		Monthly monthly= new Monthly();
        		
        		System.out.println("Please enter a description:");
        		description = keyboard.nextLine();
        		monthly.setDescription(description);
        		
        		System.out.println("Please enter a time:(military)");
        		time = keyboard.nextInt();
        		monthly.setTime(time);
        		
        		System.out.println("Please enter the year:");
        		year = keyboard.nextInt();
        		
        		System.out.println("Please enter the name of the month:");
        		month = keyboard.next();
        		
        		System.out.println("Please enter the day:");
        		day = keyboard.nextInt();
        		
        		keyboard.nextLine();
        		System.out.println("Please enter the day of the week:");
        		weekDay = keyboard.nextLine();
        		
        		Date temp = new Date(year,month, time, weekDay);
        		monthly.setDate(temp);
        		
        		calendar[insertPos] = monthly;
        		insertPos++;
        		
        	}else{
        		System.out.println("Invalid Input");
        	}
        }//while(isContinue)
        
        
        for(int i = 0; i < MAX_SIZE; i++){
        	if(calendar[i] != null){
        	calendar[i].saveAppData(output);
        	}else{
        		break;
        	}
        }
        output.close();
        
        System.out.println("************************************");
        System.out.println("Loading data from file...");
        count = 0;
        while(inStream.hasNext()){
        	
        	if(count < MAX_SIZE){
        		
        		
        		input = inStream.nextLine();
        		
        		if(input.equalsIgnoreCase("One Time")){

        			OneTime oneTime = new OneTime();
        		    loadData[count] = oneTime.loadAppData(inStream);
        			count++;
        		}else if(input.equalsIgnoreCase("Daily")){
        			
        			Daily daily = new Daily();
        		    loadData[count] = daily.loadAppData(inStream);
        		    count++;
        		}else if(input.equalsIgnoreCase("Weekly")){
        			
        			Weekly weekly = new Weekly();
        		    loadData[count] = weekly.loadAppData(inStream);
        		    count++;
        		}else if(input.equalsIgnoreCase("Monthly")){
        			
        			Monthly monthly = new Monthly();
        		    loadData[count] = monthly.loadAppData(inStream);
        		    count++;
        		}
        		//inStream.nextLine();
        		
        	}
        	
        }
        inStream.close();
        
        System.out.println();
        System.out.println("************************************");
    	System.out.println("Appointments loaded from file:");
        for(int i = 0; i < MAX_SIZE; i++){
        	
        	if(loadData[i] != null){
        		loadData[i].displayAppointment();
        	}else{
        		break;
        	}
        }
        
	}//main

}//class TestAppointment
